/**
 * 
 */
package com.eagle.coders.common.ui.form.renderer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.swing.JComponent;
import javax.swing.JList;

import org.dom4j.Element;

import com.eagle.coders.common.bean.annotations.type.StringInputCaseType;
import com.eagle.coders.common.bean.annotations.type.TextFiledInputDataType;
import com.eagle.coders.common.form.annotations.type.UIComponentType;
import com.eagle.coders.common.ui.form.renderer.cache.ComboboxDisplayBean;
import com.eagle.coders.common.ui.form.renderer.cache.PropertyConstraintsBean;
import com.eagle.coders.common.ui.form.renderer.cache.TableInfoBean;
import com.eagle.coders.common.ui.form.renderer.cache.UIDomainPropertyBean;

/**
 * @author Anees
 * 
 */
public class UiWidgets {

	private static final String alpha = "ABCDEFGHIJKLMNONPQRSTUVWXYZ";

	private static final String numeric = "1234567890";

	private static final String alphaNumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

	private SortedMap<Integer, String> uiDomainMap;

	private Map<String, String> colNameTypeMap;

	private Map<Integer, String> colTypeMap;

	/**
	 * 
	 * @param widgetNode
	 * @param type
	 */
	public static void createWidgetNode(Element widgetNode, String type,
			UIDomainPropertyBean propertyHolder) {

		if (null != widgetNode) {

			Element typeNode = widgetNode.addElement("node-type");

			typeNode.addAttribute("type", type);

			if (null != propertyHolder) {
				
				typeNode.addAttribute("target-bean", "");

				if ("password".equals(type)) {
					String passwordEncryption = propertyHolder
							.getPasswordEncryption();
					typeNode.addAttribute("encryption", passwordEncryption);
				} else
					typeNode.addAttribute("encryption", "");

				Element validationNode = widgetNode
						.addElement("node-validation");

				PropertyConstraintsBean constraintHolder = propertyHolder
						.getConstraints();

				if (null != constraintHolder) {

					if (null == constraintHolder.getMaxLength()) {
						validationNode.addAttribute("max-length", "10");
					} else {
						validationNode.addAttribute("max-length",
								constraintHolder.getMaxLength().toString());
					}

					if (null == constraintHolder.getInputCaseType()) {
						validationNode.addAttribute("case-type", "mixed");
					} else {

						if (StringInputCaseType.MIXEDCASE
								.equals(constraintHolder.getInputCaseType())) {
							validationNode.addAttribute("case-type", StringInputCaseType.MIXEDCASE.toString());
						} else if (StringInputCaseType.UPPERCASE
								.equals(constraintHolder.getInputCaseType())) {
							validationNode.addAttribute("case-type", StringInputCaseType.UPPERCASE.toString());
						} else if (StringInputCaseType.LOWERCASE
								.equals(constraintHolder.getInputCaseType())) {
							validationNode.addAttribute("case-type", StringInputCaseType.LOWERCASE.toString());
						}
					}

					if (null == constraintHolder.getTextInputDataType()) {
						validationNode.addAttribute("input-type",
								TextFiledInputDataType.ALPHANUMERIC.toString());
					} else {
						if (TextFiledInputDataType.ALPHANUMERIC
								.equals(constraintHolder.getTextInputDataType())) {
							validationNode.addAttribute("input-type",
									TextFiledInputDataType.ALPHANUMERIC.toString());
						} else if (TextFiledInputDataType.ONLYALPHA
								.equals(constraintHolder.getTextInputDataType())) {
							validationNode.addAttribute("input-type", TextFiledInputDataType.ONLYALPHA.toString());
						} else if (TextFiledInputDataType.ONLYNUMBER
								.equals(constraintHolder.getTextInputDataType())) {
							validationNode
									.addAttribute("input-type", TextFiledInputDataType.ONLYNUMBER.toString());
						}
					}

					if (null == constraintHolder.getAllowNull()) {
						validationNode.addAttribute("allow-null", "true");
					} else {
						if (constraintHolder.getAllowNull()) {
							validationNode.addAttribute("allow-null", "true");
						} else {
							validationNode.addAttribute("allow-null", "false");
						}

					}
				} else {

					validationNode.addAttribute("max-length", "10");

					validationNode.addAttribute("case-type", "mixed");

					validationNode.addAttribute("input-type", "alpha-numeric");

					validationNode.addAttribute("allow-null", "true");
				}

			}
		}
	}
	
	/**
	 * 
	 * @param widgetNode
	 * @param type
	 * @param propertyHolder
	 */
	public static void createTextAreaNode(Element widgetNode, String type,
			UIDomainPropertyBean propertyHolder){
		if (null != widgetNode) {

			Element typeNode = widgetNode.addElement("node-type");

			typeNode.addAttribute("type", type);
			
			if (null != propertyHolder) {
				String row = String.valueOf(propertyHolder.getTextAreaRow());
				String col = String.valueOf(propertyHolder.getTextAreaColumns());
				
				String maxLength = row+":"+col;
				
				typeNode.addAttribute("target-bean", "");
				
				typeNode.addAttribute("encryption", "");
				
				Element validationNode = widgetNode.addElement("node-validation");
				
				validationNode.addAttribute("max-length", maxLength);

				validationNode.addAttribute("case-type", "mixed");

				validationNode.addAttribute("input-type", "alpha-numeric");

				validationNode.addAttribute("allow-null", "true");
			}

		}
	}

	/**
	 * 
	 * @param propertyHolderList
	 * @param widgetNode
	 * @param type
	 */
	public static void createRadioButtonNode(
			List<UIDomainPropertyBean> propertyHolderList, Element widgetNode,
			String type) {

		if (null != widgetNode) {

			Element typeNode = widgetNode.addElement("node-type");

			typeNode.addAttribute("type", type);
			
			typeNode.addAttribute("target-bean", "");

			Element radioBtnNode = typeNode.addElement("radioButton");

			int radioPosition = 1;

			for (UIDomainPropertyBean propertyHolder : propertyHolderList) {

				Element radio = radioBtnNode.addElement("radio");

				radio.addAttribute("label", propertyHolder.getUiPropertyLabel());

				radio.addAttribute("position", String.valueOf(radioPosition));

				radio.addAttribute("choice", propertyHolder
						.getRadioButtonChoice().toString());

				radioPosition++;
			}
		}
	}

	/**
	 * 
	 * @param widgetNode
	 * @param type
	 * @param propertyHolder
	 */
	public static void createComboboxNode(Element widgetNode, String type,
			UIDomainPropertyBean propertyHolder) {
		if (null != widgetNode) {

			Element typeNode = widgetNode.addElement("node-type");

			typeNode.addAttribute("type", type);
			if(null != propertyHolder.getTargetClazzName()){
				String clazzName = propertyHolder.getTargetClazzName();
				typeNode.addAttribute("target-bean", clazzName);

				if(propertyHolder.isTargetEnumerated()){
					typeNode.addAttribute("target-type", "enum");
					typeNode.addAttribute("enum-type", propertyHolder.getEnumType());
				}else{
					typeNode.addAttribute("target-type", "class");
					typeNode.addAttribute("enum-type", "");
				}
	
				
				ComboboxDisplayBean comboboxDisplayBean = propertyHolder.getComboboxDisplayBean();
				if(null != comboboxDisplayBean){

					Element comboBoxDisplayNode = typeNode.addElement("combobox-display");
					comboBoxDisplayNode.addAttribute("idention-style", comboboxDisplayBean.getIdentionType());
					SortedMap<Integer, String> compDisplayMap = comboboxDisplayBean.getDisplayComponentMap();
					Set<Integer> keyPositions = compDisplayMap.keySet();
					for(Integer key : keyPositions){
						
						Element comboboxDisplayCompNode = comboBoxDisplayNode.addElement("display-component");
						
						comboboxDisplayCompNode.addAttribute("comp-position", String.valueOf(key));
						comboboxDisplayCompNode.addAttribute("comb-component", compDisplayMap.get(key));
					}
				}
				
			}
		}
	}

	/**
	 * 
	 * @param widgetNode
	 * @param type
	 * @param entityClass
	 */
	public static void createTableNode(Element widgetNode, String type,
			Class entityClass, UIDomainPropertyBean propertyHolder) {

		if (null != widgetNode) {

			
			Class<?> targetBean = propertyHolder.getRelationshipClassProperty();
			
			List<TableInfoBean> tableInfoBeanList = propertyHolder.getTableInfoBeanList();
			
			Element typeNode = widgetNode.addElement("node-type");

			typeNode.addAttribute("type", type);
			typeNode.addAttribute("target-bean", entityClass.getName());

			typeNode.addAttribute("target-type", "class");
			typeNode.addAttribute("enum-type", "");

			String clzzName = BeansAnnotationProcessor
					.getClassName(entityClass);

			Element tableNode = typeNode.addElement("table");

			TableWidget tableWidget = new TableWidget();

//			TODO:
			if(null != tableInfoBeanList && tableInfoBeanList.size() > 0){
				
				System.out.println("[Renderer][UiWidget] :: tableInoBeanList ===================== 1");
				List<UIDomainPropertyBean> uiDomainPropertyHolderList = new ArrayList<UIDomainPropertyBean>();
				
				for(TableInfoBean tableInfoBean : tableInfoBeanList){
					UIDomainPropertyBean domainPropertyBean = new UIDomainPropertyBean();
					
					tableInfoBean.getProperty();
					tableInfoBean.getPosition();
					UIComponentType componentType = tableInfoBean.getComponentType();
					
					/* == Target Bean can be used to mixed table columns from different combinition of beans == */
					tableInfoBean.getTargetBean();//TODO: Not implemented yet
					
					domainPropertyBean.setTableDisplayPosition(Integer.valueOf(tableInfoBean.getPosition()));

					if(UIComponentType.COMPONENT_NULL.equals( componentType)){
						BeanRenderer.processBeansProperty(targetBean, tableInfoBean.getProperty(), domainPropertyBean);
					}else{
						domainPropertyBean.setUiPropertyLabel(tableInfoBean.getProperty());
						domainPropertyBean.setUiPropertyType(componentType.toString());
					}
					
					uiDomainPropertyHolderList.add(domainPropertyBean);
				}
				
				tableWidget.createTable(uiDomainPropertyHolderList, tableNode); // Create Table widget
				
			}else{
				
//				System.out.println("[Renderer][UiWidget] :: NORMAL FROM RELATION ===================== 2");

				List<UIDomainPropertyBean> uiDomainPropertyHolderList = BeanRenderer.getBeanIntrospection(entityClass);
				
				tableWidget.createTable(uiDomainPropertyHolderList, tableNode); // Create Table widget
				
			}
//			List<UIDomainPropertyBean> uiDomainPropertyHolderList = BeanRenderer
//					.getBeanIntrospection(entityClass);
//
//			Element tableNode = typeNode.addElement("table");
//			
//			
//
//			TableWidget tableWidget = new TableWidget();
//
//			tableWidget.createTable(uiDomainPropertyHolderList, tableNode);

		}
	}

}